package com.example.shop_car_2208.search

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.shop_car_2208.entity.Data
import com.example.shop_car_2208.entity.GoodsResult
import com.example.shop_car_2208.mvvm.BaseViewModel
import kotlinx.coroutines.launch

class SearchViewModel:BaseViewModel() {
    //定义搜索成功的返回值--接收的是一个list集合（不包含code和message）

    val searchSuccess = MutableLiveData<List<Data>>()
    val searchFail = MutableLiveData<String>()
    fun search(name:String){
        viewModelScope.launch {
            try {
                val result = apiService.getGoods(0,1,100)
                val data = result.data.filter { name in it.goods_desc }
                searchSuccess.value = data

            }catch (e:Exception){
                searchFail.value = e.message
            }
        }
    }

}